package cn.nawang.ebeim.api.utils;

/**
 * @ClassName: ConvertUtil
 * @Description: 参数转换工具
 * @author fanyb
 * @date Aug 26, 2015 11:33:56 AM
 * @version v1.0
 */
public class ConvertUtil {
  public static String toString(String[] values) {
    String value = "";
    if (values != null) {
      for (int i = 0; i < values.length; i++) {
        value = value + "[" + values[i] + "]";
      }
    }
    return value;
  }

  public static String toString(long[] values) {
    String value = "";
    if (values != null) {
      for (int i = 0; i < values.length; i++) {
        value = value + "[" + values[i] + "]";
      }
    }
    return value;
  }

  public static String toString(int[] values) {
    String value = "";
    if (values != null) {
      for (int i = 0; i < values.length; i++) {
        value = value + "[" + values[i] + "]";
      }
    }
    return value;
  }

  public static long[] toLongArray(String parameter) {
    long[] value = new long[0];
    if ((parameter == null) || (parameter.length() < 2)) return value;
    try {
      String[] strArray = parameter.substring(1, parameter.length() - 1).split("\\]\\[");
      if (strArray == null) {
        return value;
      }
      value = new long[strArray.length];
      for (int i = 0; i < strArray.length; i++) {
        try {
          value[i] = Long.parseLong(strArray[i]);
        } catch (NumberFormatException ex1) {
          value[i] = 0L;
        }
      }
      return value;
    } catch (Exception ex) {}
    return value;
  }

  public static int[] toIntArray(String parameter) {
    int[] value = new int[0];
    if ((parameter == null) || (parameter.length() < 2)) return value;
    try {
      String[] strArray = parameter.substring(1, parameter.length() - 1).split("\\]\\[");
      if (strArray == null) {
        return value;
      }
      value = new int[strArray.length];
      for (int i = 0; i < strArray.length; i++) {
        try {
          value[i] = Integer.parseInt(strArray[i]);
        } catch (NumberFormatException ex1) {
          value[i] = 0;
        }
      }
      return value;
    } catch (Exception ex) {}
    return value;
  }

  public static String[] toStringArray(String parameter) {
    String[] value = new String[0];
    if ((parameter == null) || (parameter.length() < 2)) return value;
    if (parameter.indexOf("[") < 0) {
      String[] ret = {parameter};
      return ret;
    }
    try {
      return parameter.substring(1, parameter.length() - 1).split("\\]\\[");
    } catch (Exception ex) {}
    return value;
  }

  public static String toDbString(String[] values) {
    String text = "(";
    for (int i = 0; i < values.length; i++) {
      if (i == 0)
        text = text + "'" + values[i] + "'";
      else
        text = text + ",'" + values[i] + "'";
    }
    text = text + ")";
    return text;
  }
  
  public static String toDbString(int[] values) {
    String text = "(";
    for (int i = 0; i < values.length; i++) {
      if (i == 0)
        text = text + "'" + values[i] + "'";
      else
        text = text + ",'" + values[i] + "'";
    }
    text = text + ")";
    return text;
  }

  public static String replace(String source, String find, String replacewith) {
    if ((source == null) || (find == null)) {
      return source;
    }
    int index = source.indexOf(find);
    if (index == -1) {
      return source;
    }
    int nOldLength = find.length();
    if (nOldLength == 0) {
      return source;
    }
    int indexStart = index + nOldLength;
    StringBuffer strDest = new StringBuffer(0);
    strDest.append(source.substring(0, index) + replacewith);
    while ((index = source.indexOf(find, indexStart)) != -1) {
      strDest.append(source.substring(indexStart, index) + replacewith);
      indexStart = index + nOldLength;
    }
    strDest.append(source.substring(indexStart));
    return strDest.toString();
  }
}
